package com.sdry.web.controller.llm;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import net.sf.json.JSONObject;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.sdry.model.llm.Cost;
import com.sdry.model.llm.Item;
import com.sdry.model.zc.ZcSysUserEntity;
import com.sdry.service.llm.CostService;
import com.sdry.service.llm.ItemService;
import com.sdry.utils.DateUtil;
import com.sdry.utils.ResponseUtil;
/**
 *
 *@ClassName: CostController
 *@Description: 成本费用
 *@Author llm
 *@Date 2019-07-05 15:25:38
 *@version 1.0
*/
@Controller
@RequestMapping("/Cost")
public class CostController{
	@Resource CostService costService;
	@Resource ItemService itemService;
	/** 
	 * 进入页面
	 * @param model
	 * @return 页面路径
	*/
	@RequestMapping("/page")
	public String pageCost (Model model) {
		Item item = new Item();
		List<Item> items = itemService.queryAllByMution(item );
		model.addAttribute("items",items);
		return "/td/Cost";
	}
	/** 
	 * 根据主键id查询实体
	 * @param id 主键id
	 * @return 实体
	*/
	@RequestMapping("/queryById")
	@ResponseBody
	public Cost queryById(Long id) {
		Cost param = costService.queryById(id);
		return param;
	}
	/** 
	 * 条件查询所有
	 * @param param 实体条件
	 * @return 实体集合
	*/
	@RequestMapping("/queryAllByMution")
	@ResponseBody
	public List<Cost> queryAllByMution(Cost param) {
		List<Cost> list = costService.queryAllByMution(param);
		return list;
	}
	/** 
	 * 条件分页查询
	 * @param param 实体条件
	 * @param response
	 * @throws Exception
	*/
	@RequestMapping("/list")
	public void list(Cost param,HttpServletResponse response) throws Exception{
		/*
		 * 条件分页查询内容
		 */
		List<Cost> list = costService.findPageByMution(param);
		/*
		 * 条件查询总条数
		 */
		Integer total = costService.selectCountByMution(param);
		/*
		 * 转为json数据传至页面
		 */
		//创建result
		JSONObject jsonObject = new JSONObject();
		//添加入jsonObject
		jsonObject.put("code", 0);
		jsonObject.put("rows", list);
		jsonObject.put("total", total);
		try {
			ResponseUtil.write(response, jsonObject);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	/** 
	 * 插入返回id
	 * @param param 实体条件
	 * @param response
	 * @throws Exception
	*/
	@RequestMapping("/insert")
	public void insert(Cost param,HttpSession session, HttpServletResponse response) throws Exception{
		//获取当前操作人
		ZcSysUserEntity su = (ZcSysUserEntity)session.getAttribute("user");
		Long uid = su.getId();
		param.setCreateUserId(uid);
		String dateFormat1 = DateUtil.dateFormat1();
		param.setCreateDate(dateFormat1);
		Long id = costService.insert(param);
		response.getWriter().print(id);
	}
	/** 
	 * 根据主键更新 返回影响行数
	 * @param param 实体条件
	 * @param response
	 * @throws Exception
	*/
	@RequestMapping("/update")
	public void update(Cost param,HttpServletResponse response) throws Exception{
		Integer count = costService.update(param);
		response.getWriter().print(count);
	}
	/** 
	 * 根据主键拼接的字符串删除返回影响行数
	 * @param ids 主键拼接的字符串
	 * @return 影响行数
	*/
	@RequestMapping("/delete")
	public void delete(String ids,HttpServletResponse response) throws Exception{
		ids=ids.replace("'",""); 
		Integer count = costService.delete(ids);
		response.getWriter().print(count);
	}
}
